const pool = require('../tools/getPool');
const sendResponse = require('../tools/sendResponse');

module.exports = function(req, res){

	let account = req.body.account;
	let name = req.body.name;
	let department = req.body.department;
	let able = req.body.able;

	let sql = '';

	pool.getConnection(function(err, connection) {
		if (err) { 
			sendResponse('error','database error', null, res);
			return;
		}
		let params = [];

		// if (account != undefined) {
		// 	sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk WHERE account = ?';
		// 	params = [account];
		// } else if (name != undefined && department == undefined) {
		// 	name = "%" + name + "%";
		// 	sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk WHERE name LIKE ? ';
		// 	params = [name];
		// } else if (name == undefined && department != undefined) {
		// 	sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk WHERE department = ?';
		// 	params = [department];
		// } else if (name != undefined && department != undefined) {
		// 	name = "%" + name + "%";
		// 	sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk WHERE department = ? and  name LIKE ? ';
		// 	params = [department, name];
		// } else {
		// 	sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk';
		// }


		if (able == 1) {
			sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk WHERE able = 1';
		} else {
			sql = 'SELECT id, account, password, department, name, able, createtime FROM clerk';
		}


		connection.query(sql, params, function (error, rows, fields) {

			connection.release();

			if (error) { 
				sendResponse('error','database error', null, res);
			} else {
				let result = {
					data: rows
				}
				sendResponse('ok','success', result, res);
			}
			return ;
		})	

	});

}